package com.net.xpay.core.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.FlexibleStaffCard;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.FlexibleStaffCardManager;
import com.net.common.exception.BusinessException;
import com.net.common.util.SerializationUtil;
import com.net.xpay.alipay.enums.IdentityType;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.ApplyFlexibleStaffCardModel;
import com.net.xpay.core.model.FlexibleStaffCardRepaymentTransferModel;
import com.net.xpay.core.utils.MoneyCheckUtil;
import com.net.xpay.core.utils.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class FlexibleStaffCardValidator {

    @Autowired
    private FlexibleStaffCardManager flexibleStaffCardManager;

    public void validateApplyFlexibleStaffCard(User user, ApplyFlexibleStaffCardModel model) {
        log.info("validateApplyFlexibleStaffCard userId={}|model={}", user.getId(), SerializationUtil.obj2String(model));

        if (Strings.isNullOrEmpty(model.getIdentity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开卡方账号不能为空");
        }

        if (Strings.isNullOrEmpty(model.getIdentityType())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开卡方账号类型不能为空");
        }

        if (IdentityType.ALIPAY_LOGON_ID.getValue().equals(model.getIdentityType())) {
            if (Strings.isNullOrEmpty(model.getIdentityName())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开卡方账号名称不能为空");
            }
        }

        if (Strings.isNullOrEmpty(model.getImportBizNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号不能为空");
        }
        if (model.getImportBizNo().length() > 64) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号长度不能超过64");
        }

        FlexibleStaffCard card = flexibleStaffCardManager.findByImportBizNo(user.getId(), model.getImportBizNo());
        if (card != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号已被使用");
        }

        if (Strings.isNullOrEmpty(model.getHireIdentity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "雇佣方账号不能为空");
        }

        if (Strings.isNullOrEmpty(model.getHireIdentityType())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "雇佣方账号类型不能为空");
        }

        if (IdentityType.ALIPAY_LOGON_ID.getValue().equals(model.getHireIdentityType())) {
            if (Strings.isNullOrEmpty(model.getHireIdentityName())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "雇佣方账号名称不能为空");
            }
        }

        if (model.getRepaymentAmount() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "每月额度不能为空");
        }

        if (model.getRepaymentAmount() <= 0L) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "每月额度必须大于0");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl()) && model.getNotifyUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址长度不能超过255个字");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl())) {
            if (!UrlUtil.isHttpUrl(model.getNotifyUrl())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址格式不正确");
            }
        }

        if (!Strings.isNullOrEmpty(model.getReturnUrl()) && model.getReturnUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "跳转地址长度不能超过255个字");
        }
        if (!Strings.isNullOrEmpty(model.getReturnUrl())) {
            if (!UrlUtil.isHttpUrl(model.getReturnUrl())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "跳转地址格式不正确");
            }
        }
    }

    public void validateTransfer(User user, FlexibleStaffCardRepaymentTransferModel model) {
        log.info("validateTransfer userId={}|model={}", user.getId(), SerializationUtil.obj2String(model));

        if (Strings.isNullOrEmpty(model.getImportBizNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号不能为空");
        }
        if (model.getImportBizNo().length() > 64) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号长度不能超过64");
        }

        if (model.getType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "交易类型不能为空");
        }

        if (Strings.isNullOrEmpty(model.getLoanAgreementNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "扣款协议号不能为空");
        }

        if (Strings.isNullOrEmpty(model.getOrderTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "标题不能为空");
        }
        if (model.getOrderTitle().length() > 32) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "标题长度不能超过32");
        }
        if (!Strings.isNullOrEmpty(model.getRemark()) && model.getRemark().length() > 32) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注长度不能超过32");
        }
        if (!MoneyCheckUtil.isValidMoney(model.getTransAmount(), 2)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "转账金额格式错误 单位元 最多2位小数");
        }
        if (Strings.isNullOrEmpty(model.getIdentity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款方账号不能为空");
        }
        if (model.getIdentity().length() > 32) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款方账号长度不能超过32");
        }
        if (model.getIdentityType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款方账号类型不能为空");
        }
        if (model.getIdentityType() == IdentityType.ALIPAY_LOGON_ID) {
            if (Strings.isNullOrEmpty(model.getName())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款人姓名不能为空");
            }
            if (model.getName().length() > 128) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款人姓名长度不能超过128");
            }
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl()) && model.getNotifyUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址长度不能超过255个字");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl())) {
            if (!UrlUtil.isHttpUrl(model.getNotifyUrl())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址格式不正确");
            }
        }
    }
}
